package com.example.cfm.shakespearean;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    public static final String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public boolean isMultiPane(){
        return getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE;
    }
    /**
     * Helper function to show the details of a selected item, either by
     * displaying a fragment in-place in the current UI, or starting a
     * whole new activity in which it is displayed
     */
    public void showDetails(int index){
        Log.v(TAG, "in MainActivity showDetails( " + index + ")");

        if (isMultiPane()){
            // Check what fragment is shown, replace if needed
            DetailsFragment details = (DetailsFragment)
                    getFragmentManager().findFragmentById(R.id.details);

            if ((details == null) || (details.getShownIndex() != index)){
                // Make new fragment to show this selection.
                details = DetailsFragment.newInstance(index);

                // Execute a transaction replacing any existing
                // fragment with this one inside the frame
                FragmentTransaction ft =
                        getFragmentManager().beginTransaction();


                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.replace(R.id.details, details)
                        .commit();
            }
        } else {
            // Otherwise you need to launch a new activity to display
            // the dialog fragment with selected text
            Intent intent = new Intent();
            intent.setClass(this, DetailsActivity.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }
    }


}
